package io.lkt.modules.sys.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.commons.dynamic.datasource.annotation.DataSource;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.KeywordInfoEntity;
import io.lkt.modules.sys.service.KeywordInfoService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 关键字表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2019-03-27 11:50:23
 */
@RestController
@RequestMapping("/sys/keywordinfo")
public class KeywordInfoController {
    @Autowired
    private KeywordInfoService keywordInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:keywordinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = keywordInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:keywordinfo:info")
    public R info(@PathVariable("id") Integer id){
        KeywordInfoEntity keywordInfo = keywordInfoService.getById(id);
        return R.ok().put("keywordInfo", keywordInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:keywordinfo:save")
    public R save(@RequestBody KeywordInfoEntity keywordInfo){
        String keywordContent=keywordInfo.getKeywordContent();
        int num=keywordInfoService.totalByContent(keywordInfo);
        if(num>0){
            return R.error("关键字已存在,请重新输入！");
        }
        keywordInfo.setCreateTime(new Date());
        keywordInfoService.insert(keywordInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:keywordinfo:update")
    public R update(@RequestBody KeywordInfoEntity keywordInfo){
        ValidatorUtils.validateEntity(keywordInfo);
        String keywordContent=keywordInfo.getKeywordContent();
//        int num=keywordInfoService.totalByContent(keywordInfo);
//        if(num>0){
//            return R.error("关键字已存在,请重新输入！");
//        }
        keywordInfo.setModifyTime(new Date());
        keywordInfoService.update(keywordInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:keywordinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        keywordInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
